﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PreContractWeb.Task
{
    public partial class SendEmail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Header.Controls.Add(new LiteralControl(@"<base target=""_self"" />"));

            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            //if (Page.IsPostBack)
            //{

            //    InitDate();

            //}

        }

        private void InitDate()
        {
            string nos = string.Empty;
            if (Request["no"] != null)
            {
                nos = Request["no"].ToString(); 
            }

            this.txtPreContractNo.Text = nos;

        }

        protected void btnSend_Click(object sender, EventArgs e)
        {

            try
            {
                //string[] ids = Request["id"].ToString().Split(",".ToCharArray());

                //joan 2008-10-22 get ids from hidden textbox.
                string[] ids = this.hidIds.Value.ToString().Split(",".ToCharArray());

                BEGDC.PreContract.BLL.IPreContractInfo precontractService = (BEGDC.PreContract.BLL.IPreContractInfo)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_PreContractInfo_Service];
                foreach (string id in ids)
                {
                    if (id == string.Empty)
                        continue;

                    BEGDC.PreContract.DAL.PreContractInfo precontract = precontractService.GetPreContractInfo(Convert.ToInt32(id));

                    if (precontract != null )
                    {
                        BEGDC.PreContract.DAL.Email email = new BEGDC.PreContract.DAL.Email();
                        email.PreContractID = precontract.PreContractID;
                        email.ClassName = BEGDC.PreContract.BLL.Email.ClassName.PreContractEmail.ToString();
                        email.EmailContent = this.txtComments.Text.Replace("\r\n","<br/>");
                        email.Subject = this.txtSubject.Text;
                        email.IsSend = false;
                        email.Create();
                    }
                }

                //this.SendEmails();

                Page.Response.Write(BaseFunction.GetAlertByReturnValue(true, string.Empty, "邮件已经进入发送列表, 稍后即会发送."));
            }
            catch (Exception ex)
            {

                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }       
    }
}
